﻿

namespace Mulala.Cavan.Domain.MainBoundedContext.MulalaModule
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Mulala.Cavan.Core.Persistent;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Core.MyLinq;
    using Mulala.Cavan.Core.Extension;

    public class CorporationSpecification : ISpecification<Corporation, CorporationSearch>
    {
        public CorporationSpecification()
        {
            this.SearchParams = new CorporationSearch();
        }

        public CorporationSearch SearchParams
        {
            get;
            set;
        }

        public System.Linq.Expressions.Expression<Func<Corporation, bool>> Expression
        {
            get 
            {
                IQueryBuilder<Corporation> queryBuilder = QueryBuilder.Create<Corporation>();

                if (null != SearchParams)
                {
                    if (SearchParams.Type.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(c => (int)c.Type, SearchParams.Type.Value.GetHashCode());
                    }
                    if (SearchParams.BusinessExperience > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.BusinessExperience, SearchParams.BusinessExperience);
                    }
                    if (SearchParams.AreaCode.IsNotEmpty())
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Area.ID.Substring(1, SearchParams.AreaCode.Length), SearchParams.AreaCode);
                    }
                    if (SearchParams.Scale > 0)
                    {
                        queryBuilder = queryBuilder.Equals(c => c.Scale, SearchParams.Scale);
                    }
                    if (null != SearchParams.SkillPosition)
                    {
                        foreach (int sp in SearchParams.SkillPosition)
                        {
                            queryBuilder = queryBuilder.Greater(c => c.Speciality & (long)Math.Pow(2, sp), 0);
                        }
                    }
                    if (SearchParams.IsSystemRecommend.HasValue)
                    {
                        if (SearchParams.IsSystemRecommend.Value)
                        {
                            queryBuilder = queryBuilder.Greater(c => c.CorporationStatus.RecommendTimeout, DateTime.Now);
                        }
                        else
                        {
                            queryBuilder = queryBuilder.Less(c => c.CorporationStatus.RecommendTimeout, DateTime.Now);
                        }
                    }
                    if (SearchParams.MinScore > 0 && SearchParams.MaxScore > 0)
                    {
                        queryBuilder = queryBuilder.Greater(c => c.UserAccount.Score, SearchParams.MinScore);
                        queryBuilder = queryBuilder.LessThanEqual(c => c.UserAccount.Score, SearchParams.MaxScore);
                    }
                    if (SearchParams.OnLineStatus.HasValue)
                    {
                        queryBuilder = queryBuilder.Equals(c => (int)c.CorporationStatus.OnLineStatus, SearchParams.OnLineStatus.Value.GetHashCode());
                    }
                }

                return queryBuilder.Expression.Expand();
            }
        }
    }
}
